package com.augurit.wf.app.fawen.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.BaseAction;
import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.sysfile.web.form.SysFileForm;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.util.ReflectionUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.wf.app.fawen.service.IOaFawenService;
import com.augurit.wf.app.fawen.web.form.OaFawenForm;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.common.constant.WfConstant;
import com.augurit.wf.common.entity.OaFawen;
import com.augurit.wf.common.service.IWfBusClientService;
import com.augurit.wf.common.util.WfUtils;
import com.augurit.wf.common.web.WfBusClientExtAction;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "oa-fawen.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/wf/app/fawen/oa-fawen.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/wf/app/fawen/oa-fawen-input.jsp"),
    @Result(name = WfConstant.MAKE_DOCUMENT, location = "/wf/client/common/make-document.jsp")
})
public class OaFawenAction extends WfBusClientExtAction<OaFawenForm>{
    
	@Autowired
	private IOaFawenService oaFawenService;
	@Autowired
    private ISysCodeService syscodeService;
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	@Autowired
	private IOmUserService omUserService;
	
	
	private OaFawenForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<OaFawenForm> page = new Page<OaFawenForm>(10);
	

    /**
     * 保存新增或者修改的公告
     */
    public String wfBusSave() throws Exception {
    	WfBusInstanceTaskForm task = this.wfBusSave((IWfBusClientService)oaFawenService, form, OaFawen.class.getSimpleName());
    	WfUtils.renderWfBusSaveJson(task, form);
        return null;
    }
    
    /**
     * 打印公告单
     */
    public String print() throws Exception {
        return WfUtils.wfBusPrint(form);
    }

	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		oaFawenService.search(page, form);
		return SUCCESS;
	}

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		this.initSysCodeItem();
		this.initWfOpinions();
        return INPUT;
	}
	
	/**
     * 制文
     * @return
     * @throws Exception
     */
    public String makeDocument() throws Exception {
    	form.setIssueDate(new Date());
    	oaFawenService.save(form);
    	
    	String templateCode = getTemplateCode();
    	if(templateCode != null && templateCode.trim().length() > 0){
    		String printTemplatePath = wfBusTemplateService.getPrintTemplatePath(templateCode);
        	form.setTemplatePath(printTemplatePath);
//        	form.setZhengwenPath(exSysFileService.getZhengwenPath(OaFawen.class.getSimpleName(), id));
        	
        	WfUtils.wfBusPrint(form);
        	
        	BaseAction.getRequest().setAttribute("entity", OaFawen.class.getSimpleName());
        	BaseAction.getRequest().setAttribute("entityId", id);
        	BaseAction.getRequest().setAttribute("busType", OaFawen.class.getSimpleName());
    	}
    	return WfConstant.MAKE_DOCUMENT;
    }
    
    public String zhiwen() throws Exception {
//    	WfUtils.wfBusPrint(form);
    	
    	Map<String, String> variables = new HashMap<String, String>();
    	try{
			
			String[] fields = ReflectionUtils.getFieldArray(form.getClass());
			
			for(String field : fields){
				Object value = ReflectionUtils.invokeGetterMethod(form, field);
				if(value != null){
					if(value instanceof Timestamp || value instanceof Date){
						Date date = (Date)value;
						variables.put(field, DateUtils.dateTimeToString(date, DateUtils.YYYY_MM_DD));
					}
					else
						variables.put(field, value.toString());
					
				}
				else
					variables.put(field, "");
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
    	
        this.renderJson(variables);
    	return null;
    }

    private void initSysCodeItem() {
        // 紧急程度
        List<SysCodeForm> urgencyList = syscodeService.getItems("OA_urgency");
        // 秘密等级
        List<SysCodeForm> secretList = syscodeService.getItems("OA_secret");

        super.getRequest().setAttribute("urgencyList", urgencyList);
        super.getRequest().setAttribute("secretList", secretList);
    }
    
    /**
     * 显示所有的文档
     * @return
     * @throws Exception
     */
    public String listAllDocuments() throws Exception{
    	List<SysFileForm> list = sysFileService.getByEntityAndEntityId(OaFawen.class.getSimpleName(), id);
    	ExtBaseAction.extRenderGridJson(list);
    	return null;
    }
    
	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save() throws Exception {
		oaFawenService.save(form);
		return RELOAD;
	}

	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		oaFawenService.delete(id);
		return RELOAD;
	}
	
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {// 拟稿人
        LoginUserForm userForm = this.getLoginUser();

		if(id != null){
			form = oaFawenService.get(id);
		} else {
			form = new OaFawenForm();
			if (userForm != null) {
	            form.setDraftPerson(userForm.getUser().getUserName());
	            
	            OmOrgForm mainOrg = omUserService.getRelatedMainOrg(userForm.getUser().getUserId());
	            if(mainOrg != null)
	            	form.setDraftDept(mainOrg.getOrgName());
	        }
		}
        if (userForm != null) {
            form.setCurrUserName(userForm.getUser().getUserName());
        }
	}
	

	public OaFawenForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<OaFawenForm> getPage() {
		return page;
	}


	public void setPage(Page<OaFawenForm> page) {
		this.page = page;
	}

	
}
